package com.gs.common.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;

//@Configuration
public class ExchangeQueueBindingConfig {

    @Autowired
    RabbitAdmin rabbitAdmin;

    //死信队列
    @Bean
    public Queue createDeadQueue() {
        return new Queue("gs.dead_queue", true);
    }

    //消息队列
    @Bean
    public Queue createMessageQueue() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("x-dead-letter-exchange", "dead_exchange");
        map.put("x-dead-letter-routing-key", "dead_letter_router_key");
        return new Queue("gs.simple.queue", true, false, false, map);
    }


    //创建初始化RabbitAdmin对象
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    //创建交换机和对列
    @Bean
    public void createExchangeQueue() {
        //死信
        rabbitAdmin.declareQueue(createDeadQueue());
        //正常
        rabbitAdmin.declareQueue(createMessageQueue());
    }
}
